﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="StaffModel" Headers="_headers" DataSource="Staffs" SortByItem="@nameof(StaffModel.PositionOfficial)" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard" Rounded
                    Predicate="s => dataTableCard==null ||string.IsNullOrWhiteSpace(dataTableCard._search) || (s.Name!=null&&s.Name.Contains(dataTableCard._search))|| (s.CustomName!=null&&s.CustomName.Contains(dataTableCard._search))|| (s.SubordinateOrganization!=null&&s.SubordinateOrganization.Contains(dataTableCard._search))|| (s.PositionOfficial!=null&&s.PositionOfficial.Contains(dataTableCard._search))">
         <ItemColContent>
             @if (context.Header.Value == "actions")
            {
                <div style="width: 140px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>EditItem(context.Item)">@IconType.Edit.ToIconString()</MIcon>
                    <MIcon Color="error"
                           Class="mr-2"
                           OnClick="()=>DeleteItem(context.Item)">@IconType.Delete.ToIconString()</MIcon>
                </div>
            }
            else if (context.Header.Value == nameof(StaffModel.PositionGeneral))
            {
                @context.Item.PositionGeneral.GetDisplayName()
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
        <ButtonContent>
            <CnGalWebSite.Components.Buttons.MasaButton Text="批量导入" Rounded Icon="@IconType.Import.ToIconString()" OnClick="BatchItems" Class="me-4" />
         </ButtonContent>
     </DataTableCard>

 </MDCard>

 <MDialog @bind-Value="_editDialog"
          MaxWidth="800">
     <EditCard Staffs="Staffs" OnEdited="OnEdited" @ref="editCard" Model="_editedItem" />
 </MDialog>
 <MDialog @bind-Value="_batchDialog"
          MaxWidth="800">
     <BatchCard OnBatched="OnBatched" @ref="batchCard" />
 </MDialog>
 @code {

    [Parameter]
    public List<StaffModel> Staffs { get; set; }

    private string _name = "Staff";

    private List<DataTableHeader<StaffModel>> _headers = new List<DataTableHeader<StaffModel>>
{
          new ()
          {
            Text= "分组",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(StaffModel.Modifier)
          },
          new (){ Text= "★官方职位", Value= nameof(StaffModel.PositionOfficial)},
          new (){ Text= "★唯一名称", Value= nameof(StaffModel.Name)},
          new (){ Text= "通用职位", Value= nameof(StaffModel.PositionGeneral)},
          new (){ Text= "隶属组织", Value= nameof(StaffModel.SubordinateOrganization)},
          new (){ Text= "自定义名称", Value= nameof(StaffModel.CustomName)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<StaffModel> dataTableCard;

    EditCard editCard;
    private bool _editDialog;
    private long _editedItemId;
    private StaffModel _editedItem;

    BatchCard batchCard;
    private bool _batchDialog;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public void EditItem(StaffModel item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        _editedItem = item;
        StateHasChanged();
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public void AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        _editedItem = new StaffModel();
        StateHasChanged();
    }

    public void BatchItems()
    {
        _batchDialog = true;
        StateHasChanged();
    }


    public async Task OnBatched(List<StaffModel> model)
    {
        //检查是否符合要求
        foreach (var item in model)
        {
            var result = item.Validate(Staffs);
            if (result.Successful == false)
            {
                await PopupService.ToastAsync(result.Error, AlertTypes.Error);
            }
            else
            {
                if (Staffs.Any())
                {
                    item.Id = Staffs.Max(s => s.Id) + 1;
                }
                else
                {
                    item.Id = 0;
                }
                Staffs.Add(item);
            }
        }
        _batchDialog = false;
        StateHasChanged();
        dataTableCard.GetDataFromApi();
    }

    public void DeleteItem(StaffModel model)
    {
        Staffs.Remove(model);
        dataTableCard.GetDataFromApi();
    }
}
